;
(function (window, angular) {
    'use strict';
    angular.module("Controller.Stock.UnsoldStockConfig", [])
        .controller("UnsoldStockConfigCtrl", ["$scope", "$state", "ybWidget", "ybHttp", "MESG_TYPE", "$timeout", "EventBus", "$filter", "UserContextService", function ($scope, $state, ybWidget, ybHttp, MESG_TYPE, $timeout, EventBus, $filter, UserContextService) {
            $scope.supplierList = [];
            $scope.condition = {
                dealerOrgName: '',
                orgId: ''
            };
            $scope.dataList = [];
            $scope.pageInfo = {
                totalPages: 1,
                totalRows: 0,
                pageSize: 10,
                curPage: 1
            };
            $scope.markInfo = {
                allChecked: false
            };
            $scope.tableConfig = {
                scrollWidth: 600,
                fixedWidth: 80,
                tableCol: [
                    {
                        type: "",
                        name: "客户代码",
                        dataField: "dealerAuthId",
                        width: 150,
                        isShow: true,
                        isFixed: false
                    },
                    {
                        type: "",
                        name: "客户名称",
                        dataField: "dealerAuthName",
                        width: 150,
                        isShow: true,
                        isFixed: false
                    },
                    {
                        type: "",
                        name: "共享状态",
                        dataField: "shareStatus",
                        width: 100,
                        isShow: true,
                        isFixed: false
                    }
                ]
            };
            $scope.tbStyle = {scrollLeft: {left: $scope.tableConfig.fixedWidth + "px"}};

            $scope.search = function (curpage) {
                getDataList(curpage);
            };

            function getDataList(curPage) {
                var service = "device";
                var reqUrl = "/companyDealer/listBySupplierId";
                var param = {};
                param.pageSize = $scope.pageInfo.pageSize;
                if (!!curPage) {
                    $scope.pageInfo.curPage = curPage;
                }
                param.pageNum = $scope.pageInfo.curPage;
                param.filters = angular.copy($scope.condition);

                ybHttp.Post(service, reqUrl, param).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return;
                    }
                    $scope.pageInfo.totalRows = resp.value.total;
                    $scope.pageInfo.totalPages = resp.value.pages;
                    $scope.dataList = resp.value.list;
                    angular.forEach($scope.dataList, function (item) {
                        item.shareStatus = fliterShareStatus(item.checked);
                    });
                    setCheckAllFlag();
                });
            }

            $scope.selectSingle = function (item) {
                if (!item.checked) {
                    $scope.markInfo.allChecked = false;
                } else {
                    setCheckAllFlag();
                }
                setDealersStatus([item.dealerAuthId], item.checked).then(function (data) {
                    if (!data) {
                        item.checked = !item.checked;
                    }
                }, function (err) {
                    item.checked = !item.checked;
                }).finally(function () {
                    item.shareStatus = fliterShareStatus(item.checked);
                });
            };

            function setCheckAllFlag() {
                if (!checkAll()) {
                    $scope.markInfo.allChecked = false;
                } else {
                    $scope.markInfo.allChecked = true;
                }
            }

            $scope.selectRow = function (index) {
                $scope.markInfo.cueRow = index;
            };

            $scope.selectAll = function () {
                var dealerAuthIds = [];
                for (var i = 0, len = $scope.dataList.length; i < len; i++) {
                    $scope.dataList[i].checked = $scope.markInfo.allChecked;
                    dealerAuthIds.push($scope.dataList[i].dealerAuthId);
                }
                if (dealerAuthIds.length <= 0) {
                    return;
                }
                setDealersStatus(dealerAuthIds, $scope.markInfo.allChecked).then(function (data) {
                    if (!data) {
                        item.checked = !item.checked;
                    }
                }, function (err) {
                    item.checked = !item.checked;
                }).finally(function () {
                    angular.forEach($scope.dataList,function (item) {
                        item.shareStatus = fliterShareStatus(item.checked);
                    });
                });
            };

            function checkAll() {
                var selectAll = false;
                for (var i = 0, len = $scope.dataList.length; i < len; i++) {
                    if (!$scope.dataList[i].checked) {
                        selectAll = false;
                        break;
                    }
                    selectAll = true;
                }
                return selectAll;
            }

            function setDealersStatus(dealerAuthIds, isShared) {
                var service = "device";
                var reqUrl = "/warehouseDealerAuth/add";
                var param = {
                    supplierId: $scope.condition.orgId,
                    dealerAuthIds: dealerAuthIds,
                    checked: isShared
                };

                return ybHttp.Post(service, reqUrl, param).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return false;
                    }
                    return true;
                });
            }

            function fliterShareStatus(checked) {
                return (checked ? "已共享" : "未共享");
            }

            function getSupplierList() {
                var service = "device";
                var reqUrl = "/companySupplier/list";
                var param = {
                    pageSize: 0,
                    pageNum: 1,
                    filters: {isPlatCompany: true}
                };
                ybHttp.Post(service, reqUrl, param).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return;
                    }
                    $scope.supplierList = resp.value.list;
                    if ($scope.supplierList.length > 0) {
                        $scope.condition.orgId = $scope.supplierList[0].platOrgId;
                    }
                });
            }

            var btnPermissionCode = {
                unsoldConfigAdd: window.ActionCode.unsoldConfigAdd
            };

            $scope.btnPermission = {
                unsoldConfigAdd: true
            };

            function setBtnPermission() {
                for (var key in btnPermissionCode) {
                    $scope.btnPermission[key] = UserContextService.CheckPermission([btnPermissionCode[key]]);
                }
            }

            (function () {
                getSupplierList();
                UserContextService.SetBtnPermission(setBtnPermission);
            })();
        }])
    ;
})(window, window.angular);